﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using BE;
using BE.SearchCriteria;

namespace DAL
{
    public class AreaDAL : BaseDAL
    {
        public List<AreaBE> SeleccionarPorParametros(BaseSC pFiltroSC, int pPageSize, int? pPageNum, ref int pCountReg)
        {
            AreaSC wFiltroSC = (AreaSC)pFiltroSC;
            if (wFiltroSC == null)
                wFiltroSC = new AreaSC();

            List<AreaBE> listaDeAreaBE = null;
            using (SqlConnection cnn = new SqlConnection(GetConnectionString()))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = cnn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@IdArea", wFiltroSC.IdArea));
                cmd.Parameters.Add(new SqlParameter("@Descripcion", wFiltroSC.Descripcion));
                cmd.Parameters.Add(new SqlParameter("@IdZona", wFiltroSC.IdZona));
                cmd.Parameters.Add(new SqlParameter("@pagesize", pPageSize));
                cmd.Parameters.Add(new SqlParameter("@pagenum", pPageNum));

                SqlParameter p = new SqlParameter();
                p.ParameterName = "@PageCount";
                p.Direction = ParameterDirection.Output;
                p.SqlDbType = SqlDbType.Int;
                cmd.Parameters.Add(p);

                cmd.CommandText = "Areas_GetAllPaginated";
                cnn.Open();
                SqlDataReader reader = null;
                reader = cmd.ExecuteReader();

                if (reader.HasRows)
                {
                    Int32 idAreaIndex = reader.GetOrdinal("IdArea");
                    Int32 descripcionIndex = reader.GetOrdinal("Descripcion");
                    Int32 idZonaIndex = reader.GetOrdinal("IdZona");
                    Int32 ZonaIndex = reader.GetOrdinal("Zona");

                    listaDeAreaBE = new List<AreaBE>();

                    while (reader.Read())
                    {
                        AreaBE oAreaBE = new AreaBE();
                        Object[] values = new Object[reader.FieldCount];
                        reader.GetValues(values);

                        oAreaBE.IdArea = Convert.ToInt32(values[idAreaIndex]);
                        oAreaBE.Descripcion = Convert.ToString(values[descripcionIndex]);
                        oAreaBE.oZonaBE = new ZonasBE();
                        oAreaBE.oZonaBE.IdZona = Convert.ToInt32(values[idZonaIndex]);
                        oAreaBE.oZonaBE.Descripcion = Convert.ToString(values[ZonaIndex]);

                        listaDeAreaBE.Add(oAreaBE);
                    }
                }
                reader.Close();
                pCountReg = (Int32)cmd.Parameters["@PageCount"].Value;

            }
            return listaDeAreaBE;
        }

        public void Insertar(AreaBE pElemBE)
        {
            using (SqlConnection cnn = new SqlConnection(GetConnectionString()))
            {
                try
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = cnn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "Areas_i";
                    cmd.Parameters.Add(new SqlParameter("@Descripcion", pElemBE.Descripcion));
                    cmd.Parameters.Add(new SqlParameter("@IdZona", pElemBE.IdZona));

                    SqlParameter p = new SqlParameter();
                    p.ParameterName = "@IdArea";
                    p.Direction = ParameterDirection.Output;
                    p.SqlDbType = SqlDbType.Int;
                    cmd.Parameters.Add(p);

                    cnn.Open();
                    cmd.ExecuteNonQuery();
                    pElemBE.IdArea = (Int32)cmd.Parameters["@IdArea"].Value;
                }
                catch (Exception)
                {
                    throw;
                }

            }
        }

        public void Modificar(AreaBE pElemBE)
        {
            using (SqlConnection cnn = new SqlConnection(GetConnectionString()))
            {
                try
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = cnn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "Areas_u";
                    cmd.Parameters.Add(new SqlParameter("@IdArea", pElemBE.IdArea));
                    cmd.Parameters.Add(new SqlParameter("@IdZona", pElemBE.IdZona));
                    cmd.Parameters.Add(new SqlParameter("@Descripcion", pElemBE.Descripcion));
                    cnn.Open();
                    cmd.ExecuteNonQuery();
                }
                catch (Exception)
                {
                    throw;
                }

            }
        }

        public void Delete(int pIdArea)
        {
            try
            {
                using (SqlConnection cnn = new SqlConnection(GetConnectionString()))
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = cnn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "Areas_d";
                    cmd.Parameters.Add(new SqlParameter("@IdArea", pIdArea));
                    cnn.Open();
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception)
            { throw; }

        }

    }
}
